import sortedcontainers
class Solution(object):
    def carPooling(self, trips, capacity):
        d = sortedcontainers.SortedDict()
        for t in trips:
            d[t[1]] = d.setdefault(t[1], 0) + t[0]
            d[t[2]] = d.setdefault(t[2], 0) - t[0]
        m = list(d.values())
        base = 0
        for num in m:
            base += num
            if base > capacity:
                return False
        return True